/**
 * @author		Ben Layet
 * @version 	1.0
 * @date		25 Sep 2008
 */
package org.ignoramus.application.model;

import java.util.HashMap;
import java.util.Map;

import org.learnaholic.application.test.JapaneseString;

/**
 * Check the answer submitted in a test.
 */
public class AnswerCheckers
{
	/** The default checker */
	private static final AnswerChecker default_answer_checker = new DefaultAnswerChecker();
	/** The map of specific checker */
	private static final Map<String, AnswerChecker>	answerCheckers	= new HashMap<String, AnswerChecker>();

	static 
	{
		//initialise the map
		answerCheckers.put("KanaChecker", new KanaAnswerChecker());	
	}
	
	/**
	 * Gets an answer checker from its name
	 * @param checkerName the checker name that can be null.
	 * @return the default checker or a specific one.
	 */
	public static AnswerChecker getChecker(String checkerName)
	{
		//try and get a specific one
		AnswerChecker ret = answerCheckers.get(checkerName);
		//if none if defined for the given name
		if(null == ret)
		{
			//set it to the default one
			ret = default_answer_checker;
		}
		//return the result
		return ret;
	}
	/**
	 * Interface for answer checkers. 
	 */
	public static interface AnswerChecker
	{
		/**
		 * Checks if an answer is correct.
		 * @param expected the expected result.
		 * @param submittedAnswer the submitted answer.
		 * @return true if the answer is considered as correct.
		 */
		public boolean isCorrect(String expected, String submittedAnswer);
	}

	/**
	 * Default answer checker: compare the strings ignoring the case. 
	 */
	public static class DefaultAnswerChecker implements AnswerChecker
	{
		/**
		 * Checks if an answer is correct.
		 * @param expected the expected result.
		 * @param submittedAnswer the submitted answer.
		 * @return true if the answer is considered as correct.
		 */
		public boolean isCorrect(String expected, String submittedAnswer)
		{
			//compare the strings ignoring the case. 
			return expected.equalsIgnoreCase(submittedAnswer);
		}
	}
	
	/**
	 * Japanese kana answer checker: compare the strings converted to official romaji. 
	 */
	public static class KanaAnswerChecker implements AnswerChecker
	{
		/**
		 * Checks if an answer is correct.
		 * @param expected the expected result.
		 * @param submittedAnswer the submitted answer.
		 * @return true if the answer is considered as correct.
		 */
		public boolean isCorrect(String expected, String submittedAnswer)
		{
			//convert to romaji. 
			expected = JapaneseString.toRomaji(expected);
			submittedAnswer = JapaneseString.toRomaji(submittedAnswer);
			
			//remove the punctuation
			expected = JapaneseString.removePunctuation(expected);
			submittedAnswer = JapaneseString.removePunctuation(submittedAnswer);
			
			//compare the results
			return expected.equals(submittedAnswer);
		}
	}

}
